package com.one.blocks.rbac.controller;

import com.one.blocks.mvc.controller.AbstractController;
import com.one.blocks.mvc.controller.valid.Custom;
import com.one.blocks.mvc.controller.valid.Update;
import com.one.blocks.mvc.controller.valid.UpdateSelective;
import com.one.blocks.mvc.request.PageRequest;
import com.one.blocks.mvc.response.ResponseHelper;
import com.one.blocks.mvc.response.WebPageResponse;
import com.one.blocks.mvc.response.WebResponse;
import com.one.blocks.rbac.constant.RbacRoleTypeConstant;
import com.one.blocks.rbac.controller.convert.SysMenuConvert;
import com.one.blocks.rbac.controller.convert.SysRoleMenuConvert;
import com.one.blocks.rbac.controller.query.SysMenuQuery;
import com.one.blocks.rbac.controller.valid.NoAllocatedData;
import com.one.blocks.rbac.controller.valid.RoleType;
import com.one.blocks.rbac.controller.vo.SysMenuVo;
import com.one.blocks.rbac.controller.vo.SysRoleGrantMenuVo;
import com.one.blocks.rbac.controller.vo.tree.SysMenuTreeNodeVo;
import com.one.blocks.rbac.domain.SysMenu;
import com.one.blocks.rbac.manager.SysMenuManager;
import com.one.blocks.rbac.manager.SysRoleManager;
import com.one.blocks.rbac.manager.SysRoleMenuManager;
import com.one.blocks.rbac.util.SubjectRoleHelper;
import com.one.blocks.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.groups.Default;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-05-01 10:06:14
 */
@Api(tags = "系统菜单权限接口")
@RestController
@RequestMapping("/system/menu")
@Slf4j
@Validated
public class SysMenuController extends AbstractController<SysMenuManager, SysMenuConvert, SysMenu, SysMenuVo, SysMenuQuery> {

    @Autowired
    private SysRoleManager sysRoleManager;

    @Autowired
    public SysMenuController(SysMenuManager manager, SysMenuConvert sysMenuConvert) {
        super(manager, sysMenuConvert);
    }

    @ApiOperation("查询单个目录")
    @PreAuthorize("system:menu:view")
    @GetMapping
    public WebResponse<SysMenuVo> get(@RequestParam Integer id) {
        return super.generalGet(id);
    }

    @ApiOperation("根据id批量查询目录")
    @PreAuthorize("system:menu:view")
    @GetMapping("/batch")
    public WebResponse<List<SysMenuVo>> getIds(@Validated @Size(min = 1, max = 100) @RequestParam(name = "ids[]") Set<Integer> ids) {
        return super.generalGetIds(ids);
    }

    @ApiOperation("分页查询目录")
    @PreAuthorize("system:menu:view")
    @GetMapping("/page")
    public WebPageResponse<SysMenuVo> page(@Validated PageRequest request, @Validated SysMenuQuery query) {
        return super.generalPage(request, query);
    }

    @ApiOperation("添加目录")
    @PreAuthorize("system:menu:insert")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Serializable> insert(@Validated({Default.class, Custom.class}) @RequestBody SysMenuVo sysMenuVo) {
        return super.generalInsert(sysMenuVo);
    }

    @ApiOperation("更新目录(更新所有字段)")
    @PreAuthorize("system:menu:update")
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Boolean> update(@Validated({Update.class, Custom.class}) @RequestBody SysMenuVo sysMenuVo) {
        return super.generalUpdate(sysMenuVo);
    }

    @ApiOperation("更新目录(只更新非空字段)")
    @PreAuthorize("system:menu:update")
    @PatchMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Boolean> updateSelective(@Validated({UpdateSelective.class, Custom.class}) @RequestBody SysMenuVo sysMenuVo) {
        // 上级目录更改 => 所有子目录都要更改
        return super.generalUpdateSelective(sysMenuVo);
    }

    @ApiOperation("删除目录")
    @PreAuthorize("system:menu:delete")
    @DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Integer> delete(@Validated @NotNull @Size(min = 1, max = 100) @NoAllocatedData(manager = SysRoleMenuManager.class) @RequestBody Set<Integer> ids) {
        return super.generalDeleteByIds(ids);
    }

    @ApiOperation("查询我的目录树(包括按钮)")
    @PreAuthorize("system:menu:view")
    @GetMapping("/tree")
    public WebResponse<List<SysMenuTreeNodeVo>> tree() {
        return ResponseHelper.success(manager.treeMenu(SubjectRoleHelper.getSysRole()));
    }

    @ApiOperation("查询指定角色目录树(包括按钮)")
    @PreAuthorize("system:menu:view")
    @GetMapping("/tree/role")
    public WebResponse<List<SysMenuTreeNodeVo>> treeOfRole(
            @Validated @NotNull @RoleType(value = {RbacRoleTypeConstant.COMMON, RbacRoleTypeConstant.PLATFORM_ADMIN}, groups = {Custom.class}) Integer roleId) {
        return ResponseHelper.success(manager.treeMenu(sysRoleManager.get(roleId)));
    }

    @ApiOperation("赋予指定角色目录权限")
    @PreAuthorize("system:menu:grant")
    @PostMapping("/grant")
    public WebResponse<Boolean> grant(@Validated({Default.class, Custom.class}) @RequestBody SysRoleGrantMenuVo sysRoleGrantVo) {
        return ResponseHelper.success(manager.grant(sysRoleGrantVo.getRoleId(), SysRoleMenuConvert.INSTANCE.grantVoToEntity(sysRoleGrantVo)));
    }
}